package app.android.sotaynauan;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.net.Uri;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.SearchManager;
import android.content.Intent;
import android.text.Html;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import app.android.database.MyDatabaseAdapter;
import app.android.model.Food;
import app.android.model.History;
import app.android.model.TypeFood;

public class FoodDetailActivity extends Activity implements OnClickListener{
	MyDatabaseAdapter mAdapter = null;
	int foodId = 0;
	TextView tvDanhMuc = null;
	TextView tvTenMon = null;
	TextView tvCongThuc = null;
	ImageView imgHinhAnh = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_food_detail);
		
		mAdapter = new MyDatabaseAdapter(this);
		tvDanhMuc = (TextView) findViewById(R.id.tvDanhMuc);
		tvTenMon = (TextView) findViewById(R.id.tvTenMonAn);
		tvCongThuc = (TextView) findViewById(R.id.tvCongThuc);	
		imgHinhAnh = (ImageView) findViewById(R.id.imgHinhAnh);
		
		Bundle bundle = getIntent().getExtras();	
		foodId = bundle.getInt(MainActivity.DETAIL_ID);			
						
		ImageButton btnBack = (ImageButton) findViewById(R.id.btnBack);
		ImageButton btnForward = (ImageButton) findViewById(R.id.btnForward);
		ImageButton btnHistory = (ImageButton) findViewById(R.id.btnHistory);
		ImageButton btnAddFavourite = (ImageButton) findViewById(R.id.btnAddFavourite);
			
		btnBack.setOnClickListener(this);
		btnForward.setOnClickListener(this);
		btnHistory.setOnClickListener(this);
		btnAddFavourite.setOnClickListener(this);
		
		ActionBar actionBar = getActionBar();	    
	    actionBar.setDisplayShowHomeEnabled(false);
		
		displayDetailFood();
		
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
	    // getIntent() should always return the most recent
	    setIntent(intent);
	    
	    Bundle bundle = getIntent().getExtras();	
  		foodId = bundle.getInt(MainActivity.DETAIL_ID);	
  		displayDetailFood();    
	    
	}
	
	private void displayDetailFood () {
		//Bundle bundle = getIntent().getExtras();	
		//foodId = bundle.getInt(MainActivity.DETAIL_ID);		
				
		Food f = mAdapter.getFoodByID(foodId);	
		
		if (f != null){
			TypeFood type = mAdapter.getTypeByID(f.getTypeID());		
			tvTenMon.setText(f.getFoodName().toString());
			tvDanhMuc.setText(type.getTypeName());
			tvCongThuc.setText(Html.fromHtml(f.getDescription()), TextView.BufferType.SPANNABLE);
			
			/*String lineSep = System.getProperty("line.separator");
			String description = "";
			if (f.getDescription() != null) {		
				description = f.getDescription().replaceAll("<br />", lineSep);
				description = description.replaceAll("<br>", lineSep);			
			}*/
			
			int imgId = this.getResources().getIdentifier(f.getImagePath(), "drawable", this.getPackageName());
			imgHinhAnh.setImageResource(imgId);
			
			// Luu lich su
			DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			Date date = new Date();
			History history = mAdapter.getHistoryByFoodID(foodId);
			
			if (history == null || history.getHisFoodId() == 0)
				mAdapter.addHistory(new History(foodId, dateFormat.format(date)));
			else
				mAdapter.updateHistory(history);
		}		
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.detail_food_menu, menu);
		return true;
	}
	
	public static void startFoodDetail(Activity activity, View source, Bundle extras) { 
		Intent launchDetailFood = new Intent(source.getContext(), FoodDetailActivity.class); 
		launchDetailFood.putExtras(extras);
		activity.startActivity(launchDetailFood);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
	       case R.id.btnBack :    	   
	    	   if ((foodId - 1) > 0) {
	    		   foodId--;
		    	   Bundle bundle = new Bundle();
		    	   bundle.putInt(MainActivity.DETAIL_ID,(int)foodId);		    	   
		    	   FoodDetailActivity.startFoodDetail(FoodDetailActivity.this, v, bundle);
//		    	   finish();
	    	   }
	    	   break;
	       case R.id.btnForward :	    	   
	    	   if ((foodId + 1) <= mAdapter.getCountFoods()) {
	    		   foodId++;
		    	   Bundle bundle = new Bundle();
		    	   bundle.putInt(MainActivity.DETAIL_ID,(int)foodId);
		    	   FoodDetailActivity.startFoodDetail(FoodDetailActivity.this, v, bundle);		    	 
//		    	   finish();
	    	   }
	    	   break;
	       case R.id.btnHistory :
	    	   Intent launchHistory = new Intent(this, HistoryListActivity.class);			
	    	   startActivity(launchHistory);	  	   
	    	   break;
	       case R.id.btnAddFavourite :
	    	   boolean success = mAdapter.changeFavourite(foodId, 1) ;
	    	   if (success) {
	    		   String msg = "Món ăn đã được thêm vào danh sách yêu thích";
	    		   Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();	    		   
	    	   }	 
	    	   break;
		}
		
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.menu_list_all:     
			Intent launchListAll = new Intent(this, MainActivity.class);			
			startActivity(launchListAll);			
			break;				
		}
		return true;
	}

}
